// Admin.js
import React, { useState, useEffect } from 'react';
import Web3 from 'web3';
import { useParams, Link } from 'react-router-dom';
// import '../static/css/app.css'; // 导入 CSS 文件
// import '../static/assets/bootstrap/css/bootstrap.min.css';
// import '../static/assets/fonts/ionicons.min.css';
// import '../static/assets/css/pikaday.min.css';
import UserListABI from '../contract/ABIs/UserList.json'; // 用户列表合约 ABI
import AdminABI from '../contract/ABIs/Admin.json'; // 管理员合约 ABI
import UserListAddress from '../contract/ADDRESSes/UserListAddress'; // 用户列表合约地址
import AdminNavBar from './nav/AdminNavBar.js';
// import '../static/indexcss/aacsss.css';
import Eye from './Eye.js'
import Admin_integralNavBar from './nav/Admin_integralNavBar.js'



const ResearchPoint = () => {
  const [web3, setWeb3] = useState(null);
  // 使用 useParams 获取路由参数中的地址信息
  const { address } = useParams();
  const [contractUserList, setContractUserList] = useState(null);
  const [contractAdmin, setcontractAdmin] = useState(null);
  const [isAdmin, setIsAdmin] = useState(false);
  const [userAddress, setUserAddress] = useState('');// 用户合约地址


  // 新增状态变量来存储用户积分 
  const [userPoints, setUserPoints] = useState(0);

  useEffect(() => {
    async function init() {
      // 连接到以太坊网络
      if (window.ethereum) {
        const web3Instance = new Web3(window.ethereum);
        try {
          // 请求用户授权
          await window.ethereum.enable();
          setWeb3(web3Instance);
          // 创建用户列表合约实例
          const userListInstance = new web3Instance.eth.Contract(
            UserListABI,
            UserListAddress
          );
          setContractUserList(userListInstance);

          // 创建管理员合约实例
          const adminInstance = new web3Instance.eth.Contract(
            AdminABI,
            address
          );
          setcontractAdmin(adminInstance);

          // 获取当前用户的以太坊账户地址
          const accounts = await web3Instance.eth.getAccounts();
          const currentAddress = accounts[0];
          // 获取管理员合约的 owner 地址
          const ownerAddress = await adminInstance.methods.getOwner().call();
          // 检查当前账户是否是管理员本人
          setIsAdmin(currentAddress === ownerAddress);
        } catch (error) {
          console.error(error);
        }
      } else {
        console.error('请安装以太坊浏览器插件，如 MetaMask');
      }
    }
    init();
  }, [address]);

  //管理员查询积分函数
  const AdminPoints = async () => {
    if (contractAdmin) {
      try {
        // 获取当前用户的以太坊账户地址
        const accounts = await web3.eth.getAccounts();
        const fromAddress = accounts[0];
        console.log("fromAddress:", fromAddress, "Input:", userAddress);
        // 获取用户合约地址
        const UserAddress = await contractUserList.methods.getUserAddress(userAddress).call({ from: fromAddress });
        console.log("用户合约地址为:", UserAddress);
        const result = await contractAdmin.methods.getUserPoint(UserAddress).call({ from: fromAddress });
        alert("用户积分为：" + result)
        // 更新用户积分状态 
        setUserPoints(userPoints);
        // console.log("userPoints:", userPoints);
      } catch (error) {
        console.error(error);
        alert('用户查询失败！');
      }
    };
  }

  return (
    <div>
      {/* 如果当前账户不是管理员，则显示提示信息 */}
      {/* {!isAdmin && (<div>您没有权限访问此页面。</div>)} */}
      {/* 如果当前账户不是管理员，则显示提示信息 */}
      {/* <nav className="navbar navbar-expand-lg fixed-top portfolio-navbar gradient navbar-dark">
                        <div className="container">
                            <a className="navbar-brand logo" href="#">先锋积分</a>
                            <button className="navbar-toggler" data-bs-target="#navbarNav" data-bs-toggle="collapse">
                                <span className="visually-hidden">Toggle navigation</span>
                                <span className="navbar-toggler-icon" />
                            </button>
                            <div className="collapse navbar-collapse" id="navbarNav">
                                <ul className="navbar-nav ms-auto">
                                    <li className="nav-item">
                                        <Link className="nav-link" to={`/admin/${address}`}>管理员端首页</Link>
                                    </li>
                                    <li className="nav-item">
                                        <Link className="nav-link" to={`/admin/${address}/Admin_publishActivity`}>活动发布</Link>
                                    </li>
                                    <li className="nav-item">
                                        <Link className="nav-link" to={`/admin/${address}/Admin_ActivityList`}>活动管理</Link>
                                    </li>
                                    <li className="nav-item">
                                        <Link className="nav-link" to={`/admin/${address}/Admin_ResearchPoint`}>积分管理</Link>
                                    </li>
                                    <li className="nav-item">
                                        <Link className="nav-link" to={`/admin/${address}/Admin_UserInfoList`}>用户管理</Link>
                                    </li>
                                    <li className="nav-item">
                                        <Link className="nav-link" to={`/admin/${address}/Admin_addStudents`}>添加学生</Link>
                                    </li>
                                    <li className="nav-item">
                                        <Link className="nav-link" to={`/admin/${address}/Admin_GetTotalCredit`}>计算总学分</Link>
                                    </li>
                                    
                                    <li className="nav-item">
                                        <Link className="nav-link" to={`/admin/${address}/GetTotalScore`}>计算总成绩</Link>
                                    </li>
                                    <li className="nav-item">
                                        <Link className="nav-link" to={`/admin/${address}/Admin_CourseManage`}>课程管理</Link>
                                    </li>
                                    <li className="nav-item">
                                        <Link className="nav-link" to={`/admin/${address}/Admin_addMultiCourses`}>批量添加课程</Link>
                                    </li>
                                    <li className="nav-item">
                                        <Link className="nav-link" to={`/admin/${address}/Admin_GetUserCourseInfo`}>查询用户课程信息</Link>
                                    </li>
                                    <li className="nav-item">
                                        <Link className="nav-link" to={`/admin/${address}/Admin_getStudentInfoList`}>查询用户学生信息列表</Link>
                                    </li>
                                    
                                    <li className="nav-item">
                                        <Link className="nav-link" to={`/`}>退出登录</Link>
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </nav> */}
      <Admin_integralNavBar address={address} />
      {/* 管理员界面 */}
      <main className="page hire-me-page">
        <section className="portfolio-block hire-me">
          <div className="foreground-component">
            <Eye />
            <div className="container">
              <div className="border rounded border-0 shadow-lg p-3 p-md-5"
                data-bs-theme="light"
                style={{
                  backgroundColor: 'rgba(36, 36, 41, 0.6)'
                }}
              >


                <div class="mb-3">
                  <h2>查询积分</h2>
                  <div className="mb-3">
                    <label className="form-label" htmlFor="name">用户账户地址</label>
                    <input className="form-control item" id="name" type="text" placeholder="用户地址" value={userAddress} onChange={(e) => setUserAddress(e.target.value)} />
                  </div>
                  <div className="mb-3 mt-4">
                    <button className="btn btn-primary btn-lg d-block w-100" onClick={AdminPoints}>查询</button>
                  </div>
                </div>


              </div>
            </div>
          </div>
        </section>
      </main>
      {/* )} */}
    </div>
  );
}

export default ResearchPoint;
